package org.rapan;

import java.util.Date;
import java.util.Set;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.rapan.EJB.CompanyInt;
import org.rapan.EJB.ReportInt;
import org.rapan.exceptions.CreateReportException;
import org.rapan.jpa.Company;
import org.rapan.jpa.Consultant;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Form;
import com.vaadin.ui.PopupDateField;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Window.Notification;

@SuppressWarnings("serial")
public class ReportForm extends Form {

	private Consultant consultant;

	ComboBox companyBox = new ComboBox("Company:");
	PopupDateField startDate = new PopupDateField("Start Date:");
	PopupDateField endDate = new PopupDateField("Ending Date:");
	ComboBox hoursBox = new ComboBox("Hours per day at work:");
	Button submitButton = new Button("Submit");

	public ReportForm(Consultant c) {
		super();
		this.consultant = c;
		if (fillComboBoxes()) {
			addField("company", companyBox);
			addField("startDate", startDate);
			addField("endDate", endDate);
			addField("Hours per day", hoursBox);
			addField("submit", submitButton);
			startDate.setValue(new Date());
			startDate.setDateFormat("dd-MM-yy");
			endDate.setDateFormat("dd-MM-yy");
			endDate.setValue(new Date());
			submitButton.addListener(new ClickListener() {

				public void buttonClick(ClickEvent event) {
					post();
				}

				private void post() {
					if (isValid()) {
						try {
							InitialContext context = new InitialContext();
							ReportInt repInt = (ReportInt) context
									.lookup("Consult/ReportBean/local");

							repInt.createReport(
									(Company) companyBox.getValue(),
									consultant, (Date) startDate.getValue(),
									(Date) endDate.getValue(),
									(Short) hoursBox.getValue());
							Notification not = new Notification(
									"Report Submitted successfully.");
							not.setDelayMsec(100 * 20);
							getWindow().showNotification(not);
							clearForm();

						} catch (NamingException e) {

						} catch (CreateReportException e) {
							StringBuilder strBuilder = new StringBuilder(
									"<br/>The form is invalid due to the following Reasons:<br/>");
							for (String error : e.errorSet)
								strBuilder.append(error + "<br/>");

							Notification not = new Notification(
									"Report Invalid.", strBuilder.toString(),
									Notification.TYPE_ERROR_MESSAGE);
							getWindow().showNotification(not);
						}
					}
				}
			});
		}

	}

	private boolean fillComboBoxes() {
		hoursBox.setNullSelectionAllowed(false);
		companyBox.setNullSelectionAllowed(false);
		for (short i = 1; i < 9; i++) {
			hoursBox.addItem(i);
			hoursBox.setItemCaption(i, Integer.toString(i));
		}
		try {
			InitialContext context = new InitialContext();
			CompanyInt comInt = (CompanyInt) context
					.lookup("Consult/CompanyBean/local");
			Set<Company> companySet = comInt.getAllCompanies();
			if (companySet.isEmpty()) {
				return false;
			}
			for (Company c : companySet) {
				companyBox.addItem(c);
				companyBox.setItemCaption(c, c.getName());
			}
			companyBox.select(companySet.iterator().next());
			hoursBox.select(1);

		} catch (NamingException e) {
			e.printStackTrace();
		}
	
		return true;

	}

	public void clearForm() {
		companyBox.setValue(null);
		hoursBox.setValue(1);
		startDate.setValue(new Date());
		endDate.setValue(new Date());
	}

}
